org.mentalsmash.asksusanna.Phrase = function(phrase,conf){

	var words = (function(){
		var result = [];
		
		var specialChars = [',','-'];
		var cleanedPhrase = phrase.replace(
				new RegExp('['+specialChars.join('')+']'),
				' '
		);
		
		var wordsStrs = cleanedPhrase.split(" ");
		for (var i = 0; i < wordsStrs.length; i++) {
			var w = wordsStrs[i].trim();
			var word = new org.mentalsmash.asksusanna.Word(w);
			result.push(word);
		}
		
		return result;
	})();
	
	var subphrases = (function(){
		var result = [];
		
		var length = words.length;
		
		while (length >= conf.getMinLength()) {
			(function(){
				var cursor = 0;
				
				while (cursor+length <= words.length) {
					(function(){
						var included = [];
						var subphrase = [];

						for (i = cursor; i < cursor+length; i++) {
							included.push(words[i]);
							subphrase.push(words[i].getWord());
						}
						
						cursor++;
						
						subphrase = subphrase.join(' ');
						$(included).each(function(i,w){
							w.addPhrase(subphrase);
						});
						
						result.push(subphrase);
					})();
				}
				
				length--;
			})();
		}
		
		return result;
	})();
	
	var subphrasesHits = (function(){
		var result = [];
		
		$(subphrases).each(function(){
			result.push(0);
		});
		
		return result;
	})();
	
	var subphrasesScores = (function(){
		var result = [];
		
		$(subphrases).each(function(){
			result.push(0);
		});
		
		return result;
	})();
	
	return {
		'getPhrase' : function() {
			return phrase;
		},
		'getWords' : function(){
			return words;
		},
		'getSubphrases' : function(){
			return subphrases;
		},
		'setSubphraseHits' : function(subph, hits) {
			var i = subphrases.indexOf(subph);
			if (typeof(hits) == "string") {
				hits = parseInt(hits);
			}
			subphrasesHits[i] = hits;
		},
		
		'getSubphraseHits' : function(sph) {
			var i = subphrases.indexOf(sph);
			if (i >= 0) {
				return subphrasesHits[i];
			} else {
				return 0;
			}
		},
		'setSubphraseScore' : function(subph, score) {
			var i = subphrases.indexOf(subph);
			if (typeof(score) == "string") {
				score = parseFloat(score);
			}
			//alert(subph+" : "+i+" - "+score);
			subphrasesScores[i] = score;
		},
		
		'getSubphraseScore' : function(sph) {
			var i = subphrases.indexOf(sph);
			if (i >= 0) {
				return subphrasesScores[i];
			} else {
				return 0;
			}
		},
		'computeScores' : function() {
			var that = this;
			
			$(subphrases).each(function(i,subphrase){
				var wordsCount = subphrase.wordsCount();
				var scoringFunction = conf.getScoringFunction(wordsCount);
				var hits = that.getSubphraseHits(subphrase);
				var score = scoringFunction(hits);
				that.setSubphraseScore(subphrase,score);
				
				$(words).each(function(j,word){
					if (word.getPhrases().contains(subphrase)){
						word.setScore(word.getScore()+score);
						var maxScore = conf.getMaxScore(wordsCount);
						var minScore = conf.getMinScore(wordsCount);
						word.setMaxScore(word.getMaxScore()+maxScore);
						word.setMinScore(word.getMinScore()+minScore);
					}
				});
			});
		}
	};
	
};